<?php

# -- DÉBUT NOTE DE LICENCE -------------------------------------
#
# Ce fichier fait partie de Clicpage.
#
# Copyright (c) 2010 Francois (www.clicpage.net)
# Distribué selon les termes de la licence GPL version 2.0.
# Consultez le fichier LICENCE ou
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- FIN NOTE DE LICENCE ---------------------------------------

class cpFeed
{
	
	protected $core;
	
	public $langue;
	public $titre;
	public $sousTitre;
	public $url;
	public $url_source;
	public $description;
	public $date_modification;
	public $auteur;
	public $id;
	
	protected $elements;
	
	
	public function __construct($core)
	{
		$this->core =& $core;
		
		$this->elements = array();
	}
	
	
	public function ajouterElement($element)
	{
		$this->elements[] = $element;
	}
	
	
	public function atom()
	{
		
		# Préparer les éléments
		$elmts = '';
		
		foreach($this->elements as $element)
		{
			
			$elmts.=
			sprintf('<entry>
	<title>%s</title>
	<link href="%s" rel="alternate" type="text/html" title="%s" />
	<id>%s</id>
	<updated>%s</updated>
	<author><name>%s</name></author>'."\n",
			html::escapeHTML($element['titre']),
			$element['url'],
			html::escapeHTML($element['titre']),
			$element['id'],
			cpDt::iso8601($element['date']),
			html::escapeHTML($element['auteur'])
			);

			if(!empty($element['tags'])) {
				foreach($element['tags'] as $tag) {
					$elmts.='<dc:subject>'.html::escapeHTML($tag)."</dc:subject>\n";
				}
			}
			
			$elmts.=
			'<content type="html">'.html::escapeHTML($element['extrait'])."</content>\n";
			
			if(!empty($element['url_commentaires'])) {
				$elmts.='<wfw:comment>'.$element['url_commentaires']."</wfw:comment>\n";
			}
			
			if(!empty($element['url_feed_commentaires'])) {
				$elmts.='<wfw:commentRss>'.$element['url_feed_commentaires']."</wfw:commentRss>\n";
			}
			
			$elmts.="</entry>\n";
			
		}
		
		# Code du feed
		$xml = sprintf(
'<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xml:lang="%s">
  
  <title type="html">%s</title>
  <subtitle type="html">%s</subtitle>
  <link href="%s" rel="self" type="application/atom+xml"/>
  <link href="%s" rel="alternate" type="text/html"
  title="%s"/>
  <updated>%s</updated>
  <author>
    <name>%s</name>
  </author>
  <id>%s</id>
  <generator uri="http://www.clicpage.net/">Clicpage</generator>
%s
</feed>',
		$this->langue,
		html::escapeHTML($this->titre),
		html::escapeHTML($this->description),
		$this->url,
		$this->url_source,
		html::escapeHTML($this->titre),
		cpDt::iso8601($this->date),
		html::escapeHTML($this->auteur),
		$this->id,
		$elmts
		);
		
		# Envoi au client
		http::etag($xml,http::getSelfURI());
		header('Content-Type: application/atom+xml; charset=UTF-8');
		
		echo $xml;
		exit;
		
	}
	
	
	public function rss()
	{
		
		# Préparer les éléments
		$elmts = '';
		
		foreach($this->elements as $element)
		{
			
			$elmts.=
			sprintf('<item>
	<title>%s</title>
	<link>%s</link>
	<guid isPermaLink="false">%s</guid>
	<pubDate>%s</pubDate>
	<dc:creator>%s</dc:creator>'."\n",
			html::escapeHTML($element['titre']),
			$element['url'],
			$element['id'],
			cpDt::rfc822($element['date']),
			html::escapeHTML($element['auteur'])
			);

			if(!empty($element['tags'])) {
				foreach($element['tags'] as $tag) {
					$elmts.='<category>'.html::escapeHTML($tag)."</category>\n";
				}
			}
			
			$elmts.=
			'<description>'.html::escapeHTML($element['extrait'])."</description>\n";
			
			if(!empty($element['url_commentaires'])) {
				$elmts.=
				'<comments>'.$element['url_commentaires']."</comments>\n".
				'<wfw:comment>'.$element['url_commentaires']."</wfw:comment>\n";
			}
			
			if(!empty($element['url_feed_commentaires'])) {
				$elmts.='<wfw:commentRss>'.$element['url_feed_commentaires']."</wfw:commentRss>\n";
			}
			
			$elmts.="</item>\n";
			
		}
			
		# Code du feed
		$xml = sprintf(
'<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet title="XSL formatting" type="text/xsl" href="%s" ?>
<rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>%s</title>
  <link>%s</link>
  <atom:link href="%s" rel="self" type="application/rss+xml"/>
  <description>%s</description>
  <language>%s</language>
  <pubDate>%s</pubDate>
  <copyright>%s</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Clicpage</generator>
%s
</channel>
</rss>',
		CP_URL_PUBLIQUE.'cp/rss2.xsl',
		html::escapeHTML($this->titre),
		$this->url_source,
		$this->url,
		html::escapeHTML($this->description),
		$this->langue,
		cpDt::rfc822($this->date),
		html::escapeHTML($this->copyright),
		$elmts
		);			
		
		
		# Envoi au client
		http::etag($xml,http::getSelfURI());
		header('Content-Type: application/rss+xml; charset=UTF-8');
		
		echo $xml;
		exit;	
		
	}
	

}

?>